package com.com.corejavalearnings.thread;

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by pankaj on 7/3/14.
 */
public class ThreadAndReturn {
    private static final ScheduledExecutorService scheduler =
            Executors.newScheduledThreadPool(2);


    public static void main(String... a) throws InterruptedException {
        scheduler.scheduleAtFixedRate
                (new RandomFactorialGenerator(), 0, 1, TimeUnit.SECONDS);
    }
}

    class RandomFactorialGenerator implements Runnable{
        Random random  = new Random();
        @Override
        public void run() {

            int limit = random.nextInt(10);
            System.out.print("\nRandom limit is: " + limit);

            if(limit==5){
                return;
            }

            int sum=0;
            for(int i=0; i<=limit;i++){
                sum = sum + i;
            }
            System.out.println("\nSum of first "+limit+ " numbers(x 10000) is: "+sum);

            for(int i=1;i<=1;i++){
                //sum+=sum;
                int j=i*2;
                 j=j+2;

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e){

                }
            }

        }
    }

